package com.rency.mtds.manager.controller;

import com.rency.mtds.manager.dal.entity.DataSourcePO;
import com.rency.mtds.manager.helper.ConvertHelper;
import com.rency.mtds.manager.repository.DataSourceRepository;
import com.rency.mtds.manager.spi.entity.DataSourceBO;
import com.rency.mtds.manager.spi.enums.ReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.rency.halo.framework.lang.enums.YesNo;
import org.rency.halo.framework.web.result.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: caiyu.ren
 * Created on 2019/9/6 20:17
 * @version: V1.0.0
 * @since JDK 11
 */
@Api(value = "数据源", tags = {"数据源"})
@RestController
@RequestMapping("/ds")
public class DataSourceController {

    @Autowired
    private DataSourceRepository dataSourceRepository;

    @ApiOperation(value = "查询数据源", httpMethod = "GET")
    @GetMapping(value = "/get/{appId}")
    public ApiResult<List<DataSourceBO>> get(@PathVariable(name = "appId") String appId){
        ApiResult.ApiResultBuilder<List<DataSourceBO>> builder = ApiResult.builder();
        List<DataSourcePO> entities = dataSourceRepository.find(appId);
        if(CollectionUtils.isEmpty(entities)){
            return builder.code(ReturnCode.RECORD_NOT_FOUND.getCode()).message(ReturnCode.RECORD_NOT_FOUND.getMsg()).build();
        }
        List<DataSourceBO> bos = ConvertHelper.convertDataSourcePO(entities);
        return builder.code(ReturnCode.SUCCESS.getCode()).message(ReturnCode.SUCCESS.getMsg()).data(bos).build();
    }

    @ApiOperation(value = "数据源列表", httpMethod = "GET")
    @GetMapping(value = "/list")
    public ApiResult<List<DataSourceBO>> list(){
        ApiResult.ApiResultBuilder<List<DataSourceBO>> builder = ApiResult.builder();
        List<DataSourcePO> entities = dataSourceRepository.loadAll();
        if(CollectionUtils.isEmpty(entities)){
            return builder.code(ReturnCode.RECORD_NOT_FOUND.getCode()).message(ReturnCode.RECORD_NOT_FOUND.getMsg()).build();
        }
        List<DataSourceBO> bos = ConvertHelper.convertDataSourcePO(entities);
        return builder.code(ReturnCode.SUCCESS.getCode()).message(ReturnCode.SUCCESS.getMsg()).data(bos).build();
    }

    @ApiOperation(value = "添加数据源", httpMethod = "POST")
    @PostMapping(value = "/add")
    public ApiResult<DataSourceBO> add(@RequestBody DataSourceBO dataSource){
        ApiResult.ApiResultBuilder<DataSourceBO> builder = ApiResult.builder();
        dataSource.setGmtModified(LocalDateTime.now());
        dataSource.setGmtCreate(dataSource.getGmtModified());
        DataSourcePO entity = ConvertHelper.convertDataSourceBO(dataSource);
        if(!dataSourceRepository.store(entity)){
            return builder.code(ReturnCode.FAILED.getCode()).message(ReturnCode.FAILED.getMsg()).build();
        }
        entity = dataSourceRepository.load(entity.getId());
        return builder
                .code(ReturnCode.SUCCESS.getCode())
                .message(ReturnCode.SUCCESS.getMsg())
                .data(ConvertHelper.convertDataSourcePO(entity))
                .build();
    }

    @ApiOperation(value = "移除数据源", httpMethod = "DELETE")
    @DeleteMapping(value = "/remove/{id}")
    public ApiResult<Void> remove(@PathVariable(name = "id") Long id){
        ApiResult.ApiResultBuilder<Void> builder = ApiResult.builder();
        if(!dataSourceRepository.updateEnable(id, YesNo.NO,YesNo.YES)){
            return builder.code(ReturnCode.FAILED.getCode()).message(ReturnCode.FAILED.getMsg()).build();
        }
        return builder
                .code(ReturnCode.SUCCESS.getCode())
                .message(ReturnCode.SUCCESS.getMsg())
                .build();
    }
}
